
package net.swa.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import net.swa.system.beans.entity.User;
import net.swa.system.util.SysConst;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class AuthorizeInterceptor extends MethodFilterInterceptor
{
    private static final long serialVersionUID = -435231817099494168L;

    private final Logger log = Logger.getLogger(AuthorizeInterceptor.class);

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        String adminActions = "RoleActionMenuActionUserActionMenuAction";
        ActionProxy proxy = invocation.getProxy();
        Object action = proxy.getAction();
        String actionName = action.getClass().getSimpleName();
        if (null == session)
        {
            return "login";
        }
        else
        {
            User u = (User) session.getAttribute(SysConst.CURRENT_USER);
            if (null == u)
            {
                return "login";
            }
            else
            {
                if (adminActions.contains(actionName))
                {
                    if (u.getUserType() == SysConst.USER_TYPE_ADMIN)
                    {
                        log.debug("仅管理员可访问  " + actionName);
                        return invocation.invoke();
                    }
                    else
                    {
                        return "forbid";
                    }
                }
                return invocation.invoke();
            }
        }
    }

}
